/*
 * Copyright (C) 2018 Baidu, Inc. All Rights Reserved.
 */
package com.baidu.aip.fl;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.aip.fl.model.CardBean;
import com.baidu.aip.fl.utils.SharePrefUtil;
import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.CustomListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.dg.R;

import java.util.ArrayList;

public class BangDingActivity extends AppCompatActivity {
    private OptionsPickerView pvCustomOptions;
    private ArrayList<CardBean> cardItem = new ArrayList<>();

    String sxb = "1";
    TextView tv_sx;
    LinearLayout line_sb;

    EditText et_jl;
    TextView tv_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bang_ding);
        if (!SharePrefUtil.getString("mac").equals("")) {
            Intent intent = new Intent(this, DetectLoginActivity.class);
            startActivity(intent);
            finish();
        }


        tv_sx = (TextView) findViewById(R.id.tv_sx);
        line_sb = (LinearLayout) findViewById(R.id.line_sb);
        et_jl = (EditText) findViewById(R.id.et_jl);
        tv_login = (TextView) findViewById(R.id.tv_login);
        line_sb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pvCustomOptions.show();
            }
        });
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String mac = et_jl.getText().toString().trim();
                if (mac.equals(""))

                {
                    Toast.makeText(BangDingActivity.this, "mac地址不能为空!", Toast.LENGTH_SHORT).show();
                    return;
                }


                SharePrefUtil.putString("sxb", sxb);
                SharePrefUtil.putString("mac", mac);
                Intent intent = new Intent(BangDingActivity.this, DetectLoginActivity.class);
                startActivity(intent);
                finish();

            }
        });
        iniWhCDData();
        initWHCDPicker();
    }


    private void initWHCDPicker() {//条件选择器初始化，自定义布局
        /**
         * @description
         *
         * 注意事项：
         * 自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针。
         * 具体可参考demo 里面的两个自定义layout布局。
         */
        pvCustomOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置

                CardBean item = cardItem.get(options1);
                tv_sx.setText(item.getCardNo() + "");
                sxb = item.getId() + "";
            }


        })
                .setLayoutRes(R.layout.pickerview_custom_options, new CustomListener() {
                    @Override
                    public void customLayout(View v) {
                        final TextView tvSubmit = (TextView) v.findViewById(R.id.tv_finish);
                        TextView ivCancel = (TextView) v.findViewById(R.id.iv_cancel);
                        tvSubmit.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                pvCustomOptions.returnData();
                                pvCustomOptions.dismiss();

                            }
                        });

                        ivCancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                pvCustomOptions.dismiss();
                            }
                        });


                    }
                })
                .isDialog(false)
                .build();

        pvCustomOptions.setPicker(cardItem);//添加数据


    }

    private void iniWhCDData() {
        cardItem.add(new CardBean("1", "上班卡"));
        cardItem.add(new CardBean("2", "下班卡"));

    }


}
